log using "./analysis_log", text replace 


/****************************************************************
Title: Divisive Jobs 
Author: Raluca L. Pahontu
*****************************************************************/

qui{
/********************************************************************************
							Table_of_contents
* 1. Set Directory 
* 2. Data Merging 
* 3. Data Cleaning
* 4. Data Preparation
* 5. Data Analysis
********************************************************************************/


clear all

********************************************************************************
* 							1. Set Directory
********************************************************************************

* 1. Set directory ------------------------------

*cd "yourpath"

********************************************************************************
* 							2. Data Merging 
********************************************************************************

* 2.1. Personal Dataset

do "clean_p" 

* 2.2. Household Dataset

do "clean_h"

* 2.3. Appending Personal_Interviews Dataset 

use "temp_shp99_p_user.dta" 
append using "temp_shp00_p_user.dta"

local m=2  
foreach i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14   {
	append using "temp_shp`i'_p_user"
	local m = `m' + 1
}

save "temp_shp_p_all", replace
	

* 2.4. Appending Household_Interviews Dataset

use "temp_shp99_h_user.dta", clear
append using "temp_shp00_h_user.dta"

local m=2 
foreach i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14   {
	append using "temp_shp`i'_h_user"
	local m = `m' + 1
}

save "temp_shp_h_all", replace

* 2.5. Merging Personal with Household Dataset 

use "temp_shp_h_all.dta" , clear
duplicates report idhous year 

merge 1:m idhous year using "temp_shp_p_all"
drop  _merge

foreach w in 99 00 01 02  03 04 05 06 07 08 09 10 11 12 13 14  {   
rm "temp_shp`w'_p_user.dta"
rm "temp_shp`w'_h_user.dta"

}


********************************************************************************
* 							3. Data Cleaning 
********************************************************************************

duplicates drop
drop if canton == -3 // dropping those with no canton

gen female = . 
	replace female = 0 if sex == 1
	replace female = 1 if sex == 2

bysort idpers: egen error_solve = mean(female) // solving an error in the data
qui tab error_solve
keep if error_solve == 0 | error_solve == 1


gen civil_status = . 
	replace civil_status = 1 if civsta == 1 // single
	replace civil_status = 2 if civsta == 2 // married
	replace civil_status = 3 if civsta == 3 | civsta == 4 | civsta == 5 // separated/divorced
	
gen new_educ = . 
	replace new_educ = 0 if edyear >=0 & edyear <12 & edyear !=.  // not compulsory
	replace new_educ = 1 if edyear >=12 & edyear <=13 & edyear !=.  // basic high school 
	replace new_educ = 2 if edyear >13 & edyear !=. // tertiary
	
gen household_income = eqon if eqon >=0  
gen log_household_income = log(household_income)

gen social_spending = . 
	replace social_spending= 0 if p13 == 1
	replace social_spending = 0.5 if p13 == 2
	replace social_spending = 1 if p13 == 3
	
gen spending_demand = 1 if p13 == 3 
	replace spending_demand = 0 if p13 == 1 | p13 == 2

gen owner = 1 if h29 == 2
replace owner = 0 if h29 ==  1 | h29 == 3 

replace p10 = ideo if year == 2010
replace p10 = . if p10 <0
gen left = 1 if p10>=0 & p10<5 & p10 !=.
replace left = 0 if p10>=5 & p10<=10 & p10 !=.

drop if age <16 


xtset idpers year 

gen secure = . 
	replace secure = 0 if w86== 4 | w86 == 5 | w86a == 4 | w86 == 3 | w86a == 3 
	replace secure = 1 if w86 ==2 | w86a == 2  | w86 == 1 | w86a == 1

gen work_status = .
	replace work_status = 0 if wstat == 2 | wstat == 3
	replace work_status = 1 if wstat == 1
	
gen change_job = . 
	replace change_job = 0 if w18 == 4 
	replace change_job = 1 if w18 == 1 | w18 == 2 | w18 ==3 

gen work_experience = . 
	replace work_experience = 0 if wstat ==2
	replace work_experience = 1 if work_status == 1 & l.work_status == 0 
	replace work_experience = 1 if work_status == 1 & l.work_status == 1 & change_job == 1
	
	replace work_experience = 2 if work_status == 1 & l.work_status == 1 & change_job == 0 	& l2.work_status == 0
	replace work_experience = 2 if work_status == 1 & l.work_status == 1 & change_job == 0 & l2.work_status == 1 & l.change_job == 1
	
	replace work_experience = 3 if work_status == 1 & l.work_status == 1 & change_job == 0 & l2.work_status == 1 & l.change_job == 0 & l3.work_status == 0 
	replace work_experience = 3 if work_status == 1 & l.work_status == 1 & change_job == 0 & l2.work_status == 1 & l.change_job == 0 & l3.work_status == 1 & l2.change_job == 1
	
	replace work_experience = 4 if work_status == 1 & l.work_status == 1 & change_job == 0 & l2.work_status == 1 & l.change_job == 0 & l3.work_status == 1 & l2.change_job == 0
	
	
gen part_time = . 
	replace part_time = 0 if w39 == 2
	replace part_time = 1 if w39 == 1
	
gen full_time = 1 if part_time == 0 
replace full_time = 0 if part_time == 1
		
gen job_tenure = . 
	replace job_tenure = 0 if work_experience == 1 | work_experience == 2 
	replace job_tenure = 1 if work_experience == 3 | work_experience == 4 

	
gen rho = 1 if wstat == 2 
	replace rho = 2 if (secure == 0 & job_tenure == 0 & full_time == 1) |  (secure == 0 & job_tenure == 0 & full_time == .) 
	replace rho = 2 if (job_tenure == 0 & full_time == 0 & secure == 1) | (job_tenure == 0 & full_time == 0 & secure == .) 
	replace rho = 2 if (secure == 0 & full_time == 0 & job_tenure == 1) | (secure == 0 & full_time == 0 & job_tenure == .)
	replace rho = 2 if secure == 0 & full_time == 0 & job_tenure == 0
	replace rho = 3 if (secure == 1 & job_tenure == 1 & full_time == 0) |  (secure == 1 & job_tenure == 1 & full_time == .)
	replace rho = 3 if (secure == 1 & full_time == 1 & job_tenure == 0) |  (secure == 1 & full_time == 1 & job_tenure == .)
	replace rho = 3 if (full_time == 1 & job_tenure == 1 & secure == 0 ) | (full_time == 1 & job_tenure == 1 & secure ==. )
	replace rho = 3 if full_time == 1 & job_tenure == 1 & secure == 1 

gen isco1 = is1maj if is1maj>=1 & is1maj!=. 
keep if status == 0 // keeping only those who have completed the individual questionnaire

keep social_spending spending_demand eqon is2maj isco1 is1maj rho secure part_time full_time job_tenure year idpers idhous female log_household_income age civil_status new_educ canton  p18 w32 n42 p61 owner left region 
drop if social_spending == . |  rho ==  . | female == . | log_household_income == . | age == . | civil_status == . | new_educ == . | canton == . | owner == . | left ==. | region==.   

save "shp", replace

global vars rho year spending_demand idpers female age  social_spending log_household_income owner new_educ civil_status canton
global cfe i.year i.canton  
global csim   log_household_income i.region i.new_educ 
global crest i.civil_status i.owner

********************************************************************************
* 							4. Data Preparation 
********************************************************************************

*Generate Data for Figure D4 

preserve 
gen unempl_spending = 1 if p61 == 1 
replace unempl_spending = 0.5 if p61==2
replace unempl_spending = 0 if p61 == 3 
keep social_spending unempl_spending 
save "dataunemplsocial", replace		 
restore


*Generate Data for Figure D8 
preserve
gen occup1 = is1maj if is1maj>0

gen risky = 1 if rho == 2 
replace risky = 0 if rho == 3

egen N_year_occu1 = count(risky) , by (year occup1)
egen r_year_occu1 = sum(risky) , by (year occup1)
gen risk_year_occu1 = r_year_occu1/N_year_occu1

drop if occup1==.

keep risk_year_occu*  year occup*
duplicates drop
sort occup1 year

bysort occup: gen lag = risk_year_occu1[_n-1] 
gen toplot = risk_year_occu1 -  lag
save "risk_occup_distribution", replace
restore

*Generate Data for Figure 4
preserve
egen ic = std(eqon)
qui reg spending_demand i.rho ic , cl(idpers)
qui margins, at (ic = (-1.5 (.5) 1.5) rho = (1 2 3)) asbalanced  saving("margins_res.dta", replace)

use "margins_res", clear
rename _margin ss_hat
rename _se se
rename _at1 rho
rename _at2 income_distance

keep ss_hat se rho income_distance
save "margins_income", replace 
restore


*Generate Data for Figure D6
preserve
xtset idpers year

gen rho1 = 1 if (secure == 1 & job_tenure == 1 & full_time == 0) |  (secure == 1 & job_tenure == 1 & full_time == .)
	replace rho1 = 2 if (secure == 1 & full_time == 1 & job_tenure == 0) |  (secure == 1 & full_time == 1 & job_tenure == .)
	replace rho1 = 3 if (full_time == 1 & job_tenure == 1 & secure == 0 ) | (full_time == 1 & job_tenure == 1 & secure ==. )
	replace rho1 = 4 if full_time == 1 & job_tenure == 1 & secure == 1 

qui xi: xtreg spending_demand i.rho1 $cfe $csim $crest, fe cl(idpers)
regsave _Irho1_2 _Irho1_3 _Irho1_4 using robustwithin_rho1_tmp, table(rho1test , order(regvars)) replace   ci


gen rho2 = 1  if (secure == 0 & job_tenure == 0 & full_time == 1) |  (secure == 0 & job_tenure == 0 & full_time == .) 
	replace rho2 = 2 if (job_tenure == 0 & full_time == 0 & secure == 1) | (job_tenure == 0 & full_time == 0 & secure == .) 
	replace rho2 = 3 if (secure == 0 & full_time == 0 & job_tenure == 1) | (secure == 0 & full_time == 0 & job_tenure == .)
	replace rho2 = 4 if secure == 0 & full_time == 0 & job_tenure == 0
	
qui xi: xtreg spending_demand i.rho2 $cfe $csim $crest, fe cl(idpers)
regsave _Irho2_2 _Irho2_3 _Irho2_4 using robustwithin_rho2_tmp, table(rho2test , order(regvars)) replace   ci


use robustwithin_rho1_tmp, clear

drop if var =="N"
drop if var == "r2"

gen newvar = substr(var,3,6)

replace var = "coef" if var == "_Irho1_2_coef"
replace var = "coef" if var == "_Irho1_3_coef"
replace var = "coef" if var == "_Irho1_4_coef"

replace var = "ci_upper" if var == "_Irho1_2_ci_upper"
replace var = "ci_upper" if var == "_Irho1_3_ci_upper"
replace var = "ci_upper" if var == "_Irho1_4_ci_upper"

replace var = "ci_lower" if var == "_Irho1_2_ci_lower"
replace var = "ci_lower" if var == "_Irho1_3_ci_lower"
replace var = "ci_lower" if var == "_Irho1_4_ci_lower"

drop if var == "_Irho1_2_stderr"
drop if var == "_Irho1_3_stderr"
drop if var == "_Irho1_4_stderr"

reshape wide rho, i(var) j(newvar)   string

save "robustwithin_rho1", replace

use robustwithin_rho2_tmp, clear

drop if var =="N"
drop if var == "r2"

gen newvar = substr(var,3,6)

replace var = "coef" if var == "_Irho2_2_coef"
replace var = "coef" if var == "_Irho2_3_coef"
replace var = "coef" if var == "_Irho2_4_coef"

replace var = "ci_upper" if var == "_Irho2_2_ci_upper"
replace var = "ci_upper" if var == "_Irho2_3_ci_upper"
replace var = "ci_upper" if var == "_Irho2_4_ci_upper"

replace var = "ci_lower" if var == "_Irho2_2_ci_lower"
replace var = "ci_lower" if var == "_Irho2_3_ci_lower"
replace var = "ci_lower" if var == "_Irho2_4_ci_lower"

drop if var == "_Irho2_2_stderr"
drop if var == "_Irho2_3_stderr"
drop if var == "_Irho2_4_stderr"

reshape wide rho, i(var) j(newvar)   string

save "robustwithin_rho2", replace
rm "robustwithin_rho1_tmp.dta"
rm "robustwithin_rho2_tmp.dta"
restore

*Generate Data for Figure D7
preserve
bysort rho year: egen spending_mean = mean(spending_demand)

keep rho year spending_mean
duplicates drop 

save "rdata_trends", replace
restore

*Generate Data for Figure D2
preserve

	
gen skill_specificity= . 
	replace skill_specificity = 6.17 if is2maj  == 10 | is2maj == 11
	replace skill_specificity = 2.84 if is2maj  == 12
	replace skill_specificity = 1.61 if is2maj  == 13 
	replace skill_specificity = 3.95 if is2maj  == 21
	replace skill_specificity = 2.88 if is2maj  == 22 
	replace skill_specificity = 1.30 if is2maj  == 23 
	replace skill_specificity = 3.38 if is2maj  == 24
	replace skill_specificity = 6.00 if is2maj  == 31 
	replace skill_specificity = 5.6 if is2maj  == 32 
	replace skill_specificity = 2.44 if is2maj  == 33 
	replace skill_specificity = 3.58 if is2maj  == 34 
	replace skill_specificity = 1.8 if is2maj  == 41 
	replace skill_specificity = 3.90 if is2maj  == 42 
	replace skill_specificity = 3.02 if is2maj  == 51 
	replace skill_specificity = 0.79 if is2maj  == 52 
	replace skill_specificity = 4.67 if is2maj  == 61 
	replace skill_specificity = 3.92 if is2maj  == 71 
	replace skill_specificity = 3.84 if is2maj  == 72
	replace skill_specificity = 20.42 if is2maj  == 73	
	replace skill_specificity = 9.45 if is2maj  == 74
	replace skill_specificity = 25.06 if is2maj  == 81
	replace skill_specificity = 12.28 if is2maj  == 82
	replace skill_specificity = 3.67 if is2maj  == 83
	replace skill_specificity = 7.39 if is2maj  == 91
	replace skill_specificity = 7.38 if is2maj  == 92
	replace skill_specificity = 6.46 if is2maj  == 93

gen offshore1 = . 
	replace offshore1 = -.3202965 if is2maj == 12
	replace offshore1 = -.6335766  if is2maj == 13
	replace offshore1 = 1.054536   if is2maj == 21
	replace offshore1 = -.7584511   if is2maj == 22
	replace offshore1 = .212094    if is2maj == 24
	replace offshore1 = -.1230909     if is2maj == 31
	replace offshore1 =  -.7536966    if is2maj == 32
	replace offshore1 =  .1042147    if is2maj == 34
	replace offshore1 =   .3995633  if is2maj == 41
	replace offshore1 =  -.2485054   if is2maj == 42
	replace offshore1 =  -.93718475   if is2maj == 51
	replace offshore1 =  -.89274693   if is2maj == 52
	replace offshore1 =  -.93395549   if is2maj == 71
	replace offshore1 =  -.45130891   if is2maj == 72
	replace offshore1 =  1.6602335   if is2maj == 73
	replace offshore1 =  1.1489307   if is2maj == 74
	replace offshore1 =  1.5920599   if is2maj == 81
	replace offshore1 =  2.3458116   if is2maj == 82
	replace offshore1 =  -.99854112   if is2maj == 83
	replace offshore1 =  -.8081249   if is2maj == 91
	replace offshore1 =  -.65796477   if is2maj == 93

	
gen rti = . 
	replace rti = -1.52 if is2maj == 13
	replace rti = -1.50 if is2maj == 83
	replace rti = -1.00 if is2maj == 22
	replace rti = -.82 if is2maj == 21
	replace rti = -.75 if is2maj == 12
	replace rti = -.73 if is2maj == 24
	replace rti = -.6 if is2maj == 51
	replace rti = -.44 if is2maj == 34
	replace rti = -.40 if is2maj == 31
	replace rti = -.33 if is2maj == 32
	replace rti = -.19 if is2maj == 71
	
	replace rti = 0.03 if is2maj == 91
	replace rti = 0.05 if is2maj == 52
	replace rti = 0.32 if is2maj == 81
	replace rti = 0.45 if is2maj == 93
	replace rti = 0.46 if is2maj == 72
	replace rti = 0.49 if is2maj == 82
	replace rti = 1.24 if is2maj == 74
	replace rti = 1.41 if is2maj == 42
	replace rti = 1.59 if is2maj == 73
	replace rti = 2.24 if is2maj == 41
		
save "shp_ss_off", replace
restore

*Generate Data for Figure D9

preserve
import delim "Rehm_OUR_Isco1.csv", clear 
sort iscoco1 year
gen isco1 = .
replace isco1 = 1 if iscoco1 == "1. Managers"
replace isco1 = 2 if iscoco1 == "2. Professionals"
replace isco1 = 3 if iscoco1 == "3. Technicians and associate professionals"
replace isco1 = 4 if iscoco1 == "4. Clerical support workers"
replace isco1 = 5 if iscoco1 == "5. Service and sales workers"
replace isco1 = 6 if iscoco1 == "6. Skilled agricultural, forestry and fishery workers"
replace isco1 = 7 if iscoco1 == "7. Craft and related trades workers"
replace isco1 = 8 if iscoco1 == "8. Plant and machine operators, and assemblers"
replace isco1 = 9 if iscoco1 == "9. Elementary occupations"
keep isco1 our year
save "rehm_our", replace

use social_spending year isco1 using "shp", clear 
drop if year <2002 | year>2012 
sort isco1 year
merge m:1 isco1 year using "rehm_our"
keep if _merge==3
drop _merge

bysort our isco1 year: egen spending_mean = mean(social_spending)
keep our isco1 year spending_mean
duplicates drop 
sort year
save "shp_and_rehm_our", replace


rm "rehm_our.dta"
restore
********************************************************************************
* 							5. Data Analysis 
********************************************************************************
}
do "2-tab1" // Produces Table 1
do "3-tabd1" // Produces Table D1
do "4-tabd2" // Produces Table D2
do "5-tabd3" // Produces Table D3

log close


********************************************************************************
								* End of Do-File. 
********************************************************************************
